package com.fhs.test.controller;

import com.baomidou.mybatisplus.advance.query.QueryField;
import com.baomidou.mybatisplus.advance.query.QueryFilter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.enums.DefaultFuncEnum;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.fhs.test.mapper.MobileMapper;
import com.fhs.test.mapper.StudentMapper;
import com.fhs.test.pojo.Mobile;
import com.fhs.test.pojo.School;
import com.fhs.test.pojo.Student;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
public class StudentController {
    @Resource
    private StudentMapper studentMapper;

    @Resource
    private MobileMapper mobileMapper;

    /**
     * 通过wrapper 进行join查询 关联字段使用po中配置的
     *
     * @return
     */
    @GetMapping("join1")
    public List<Student> join1() {
        // new的时候需要指定主类
        LambdaJoinQueryWrapper<Student> joinQueryWrapper = new LambdaJoinQueryWrapper<>(Student.class);
        joinQueryWrapper.innerJoin(School.class).like(School::getSchoolName, "一中")
                .end(Student.class).like(Student::getStuName, "王");
        joinQueryWrapper.innerJoin(Mobile.class);
        return studentMapper.selectList(joinQueryWrapper);
    }

    @GetMapping("loop")
    public List<Mobile> loop(){
        LambdaJoinQueryWrapper<Mobile> joinQueryWrapper = new LambdaJoinQueryWrapper<>(Mobile.class);
        joinQueryWrapper.innerJoin(Student.class);
        return mobileMapper.selectList(joinQueryWrapper);
    }

    /**
     * 通过wrapper 进行join查询 关联字段在程序中手动配置
     *
     * @return
     */
    @GetMapping("join2")
    public List<Student> join2() {
        // new的时候需要指定主类
        return studentMapper.selectList(new LambdaJoinQueryWrapper<>(Student.class)
                .innerJoin(School.class, School::getCode, Student::getSchoolCode)
                .end(Student.class));
    }

    /**
     * 通过Mybatis plus advance配合idea插件(在idea_plugin目录中的jar安装到idea中即可使用)
     * 需要配置 -Djps.track.ap.dependencies=false 不然编译会出错
     * todo 添加end方法
     * @return
     */
    @GetMapping("advance")
    public List<Student> advance() {
        return new Student().innerJoin(School.class)
                .schoolName().like("一中").end(Student.class).stuName().like("王").list();
    }

    /**
     * MP4函数支持
     *
     * @return
     */
    @GetMapping("fun")
    public List<Student> fun() {
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Student::getSchoolId).selectFun(DefaultFuncEnum.COUNT, Student::getStuCount)
                .groupBy(Student::getSchoolId);
        return studentMapper.selectList(wrapper);
    }

    /**
     * batch插入
     *
     * @return
     */
    @GetMapping("batchInsert")
    public List<Student> batchInsert() {
        List<Student> students = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            students.add(Student.builder().stuName(UUID.randomUUID().toString()).age(18).schoolId(1).build());
        }
        studentMapper.insertBatch(students);
        return students;
    }

    /**
     * 
     * batch更新 仅仅支持MYSQL和Postgresq
     *
     * @return
     */
    @GetMapping("batchUpdate")
    public int batchUpdate() {
        List<Student> students = new ArrayList<>();
        Student student1 = studentMapper.selectById(1);
        student1.setUpdateTime(new Date());
        students.add(student1);
        Student student2 = studentMapper.selectById(2);
        student2.setUpdateTime(new Date());
        students.add(student2);
        return studentMapper.updateBatch(students);
    }

    /**
     * 高级查询
     * http://localhost:8080/get?age=17&age-op=>  查询17岁以上的学生
     * http://localhost:8080/get?id=1 查询id是1的学生
     * http://localhost:8080/get?stuName=王&stuName-op=like  姓名包含王的学生
     * @param request
     * @return
     */
    @GetMapping("get")
    public List<Student> get(HttpServletRequest request){
        //todo 这个map的value 应该使用object 类型
        Map<String,Object> requestMap = new LinkedHashMap<>();
        for (String name : request.getParameterMap().keySet()) {
            requestMap.put(name,request.getParameter(name));
        }
        LambdaJoinQueryWrapper<Student> wrapper = QueryFilter.reqParam2Wrapper(Student.class,requestMap);
        return studentMapper.selectList(wrapper);
    }

    /**
     * 因为post不太好让大家看明白，所以直接把参数从java中写死了
     * 正常应该这么写 @RequestBody QueryFilter<Student> queryFilter
     * @param queryFilter
     * @return
     */
    @GetMapping("post")
    public List<Student> post( QueryFilter<Student> queryFilter){
        queryFilter = new QueryFilter<>();
        QueryField queryField = new QueryField();
        queryField.setTarget("com.fhs.test.pojo.School");
        queryField.setProperty("schoolName");
        queryField.setOperator("like");
        queryField.setValue("高新一中");
        queryFilter.getQuerys().add(queryField);
        LambdaJoinQueryWrapper<Student> wrapper = queryFilter.asWrapper(Student.class);
        return studentMapper.selectList(wrapper);
    }
}
